/*
* FaceCat图形通讯框架
* 著作权编号:2015SR229355+2020SR0266727
* 上海卷卷猫信息技术有限公司
*/

#include "stdafx.h"
#include "LinkControl.h"

static bool m_init = false;

LinkControl::LinkControl(){
    setBorderColor(FCColor_None);
    setBackColor(FCColor_None);
    m_timerID = FCView::getNewTimerID();
    m_lastPoint.x = -100;
    m_lastPoint.y = -100;
    m_relateControl = 0;
}

LinkControl::~LinkControl(){
    stopTimer(m_timerID);
    if(m_relateControl){
        if([m_relateControl isKindOfClass:[WKWebView class]]){
            WKWebView *webView = (WKWebView*)m_relateControl;
            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
        }
        [m_relateControl removeFromSuperview];
        m_relateControl = 0;
    }
}

void LinkControl::onAdd(){
    FCView::onAdd();
    startTimer(m_timerID, 20);
}

void LinkControl::onRemove(){
    FCView::onRemove();
    stopTimer(m_timerID);
    if(m_relateControl){
        if([m_relateControl isKindOfClass:[WKWebView class]]){
            WKWebView *webView = (WKWebView*)m_relateControl;
            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
        }
        [m_relateControl removeFromSuperview];
        m_relateControl = 0;
    }
}

void LinkControl::onTimer(int timerID){
    FCView::onTimer(timerID);
    if (timerID == m_timerID)
    {
        if(m_relateControl){
            if(isPaintVisible(this)){
                if(m_relateControl.hidden != NO){
                    m_relateControl.hidden = NO;
                }
            }else {
                if(m_relateControl.hidden != YES){
                    m_relateControl.hidden = YES;
                }
            }
        }
        if (m_lastPoint.x != pointToNative({0, 0}).x
                || m_lastPoint.y != pointToNative({0, 0}).y)
        {
            update();
            m_lastPoint = pointToNative({0, 0});
        }
    }
}

void LinkControl::setAttribute(const String& name, const String& value){
    if (name == L"target")
    {
        m_addingTarget = value;
    }
    else if (name == L"redirectflag1")
    {
        m_redirectFlag1 = value;
    }
    else
    {
        FCView::setAttribute(name, value);
    }
}

void LinkControl::update(){
    FCView::update();
    int width = getWidth(), height = getHeight();
    if (width > 0 && height > 0 && (int)m_addingTarget.length() > 0)
    {
        String dealTarget = m_addingTarget;
        m_addingTarget = L"";
        m_target = dealTarget;
    }
    if(m_relateControl){
        if(width < 0){
            width = 0;
        }
        if(height < 0){
            height = 0;
        }
        if(width >= 0 && height >= 0){
            FCPoint nativePoint1 = pointToNative({0, 0});
            FCPoint nativePoint2 = pointToNative({getWidth(), getHeight()});
            double scaleFactorX = 1, scaleFactorY = 1;
            getNative()->getPaint()->getScaleFactor(&scaleFactorX, &scaleFactorY);
            if(m_init){
                nativePoint1.x = (int)(nativePoint1.x / scaleFactorX);
                nativePoint1.y = (int)(nativePoint1.y / scaleFactorY);
                nativePoint2.x = (int)(nativePoint2.x / scaleFactorX);
                nativePoint2.y = (int)(nativePoint2.y / scaleFactorY);
            }else{
                nativePoint1.x = (int)(nativePoint1.x * scaleFactorX);
                nativePoint1.y = (int)(nativePoint1.y * scaleFactorY);
                nativePoint2.x = (int)(nativePoint2.x * scaleFactorX);
                nativePoint2.y = (int)(nativePoint2.y * scaleFactorY);
            }
            if (nativePoint2.x > nativePoint1.x && nativePoint2.y > nativePoint1.y)
            {
                FCSize nativeSize = getNative()->getSize();
                if(m_init){
                    [m_relateControl setFrame:CGRectMake(nativePoint1.x, nativeSize.cy / scaleFactorY - nativePoint1.y - height / scaleFactorY, (int)(width / scaleFactorX), (int)(height / scaleFactorY))];
                }else{
                    NSRect nsRect = CGRectMake(nativePoint1.x, nativeSize.cy * scaleFactorY - nativePoint1.y - height * scaleFactorY, (int)(width * scaleFactorX), (int)(height * scaleFactorY));
                    [m_relateControl setFrame:nsRect];
                }
            }else{
                if(width == 0 || height == 0){
                    [m_relateControl setFrame:CGRectMake(0, 0, 0, 0)];
                }
            }
        }
    }
}

bool LinkControl::isInit(){
    return m_init;
}

void LinkControl::setInit(bool init){
    m_init = init;
}

